﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace EmoneyWeiXinCommon
{
    public class JsonTools
    {
        public static string ToJson(object obj)
        {
            if (obj == null)
            {
                return "{}";
            }
            else
            {
                return JsonConvert.SerializeObject(obj);
            }
        }

        public static T ToObject<T>(string str)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(str);
            }
            catch (Exception)
            {
                return default(T);
            }
        }

        public static T ToObject<T>(string str, DateTimeZoneHandling dateTimeZoneHandling)
        {
            JsonSerializerSettings setting = new JsonSerializerSettings();
            setting.DateTimeZoneHandling = dateTimeZoneHandling;
            return JsonConvert.DeserializeObject<T>(str, setting);
        }
    }
}
